﻿(function () {
    // 创建一个 angularjs 服务模块
    var service = angular.module('myApp.Services', []);

    service.factory('utils', ['$http', '$modal', function ($http, $modal) {
        var data = [];

        var methods = {
            confirm: function (text) {
                return $modal.open({
                    templateUrl: '../../App/views/ExamPaper/confirmModal.html',
                    backdrop: 'static',
                    controller: 'confirmmModal',
                    resolve: {
                        items: function () {
                            return text;
                        }
                    }
                });
            },
            showItem: function (text) {
                return $modal.open({
                    templateUrl: '../../App/views/ExamPaper/showItem.html',
                    backdrop: 'static',
                    controller: 'viewModal',
                    size: 'lg',
                    resolve: {
                        items: function () {
                            return angular.copy(text);
                        }
                    }
                });
            },
            notify: function (content, type) {
                $.notify(content, {
                    type: type,
                    delay: 1000,
                    z_index: 1000000,
                    placement: {
                        from: 'top',
                        align: 'right'
                    }
                });
            },
            remove: function (list, item, fn) {
                angular.forEach(list, function (i, v) {
                    if (fn ? (fn(i, item)) : (i.$$hashKey === item.$$hashKey)) {
                        list.splice(v, 1);
                        return false;
                    }
                    return true;
                });
            },
            inArray: function (val, array, fn) {
                var has = false;
                angular.forEach(array, function (v) {
                    if (fn && fn(val, v) || val === v) {
                        has = true;
                        return false;
                    }
                    return true;
                });
                return has;
            },
            getData: function (key) {
                return data[key];
            },
            setData: function (newData) {
                $.extend(true, data, newData);
            }
        };
        return methods;
    }]);

    service.factory('viewService', ['$http', function ($http) {
        var getTemplates = function (itemType) {
            var template = '';
            switch (itemType) {
                case 1:
                    template = $http.get('../../App/views/ExamPaper/single.html');
                    break;
                case 2:
                    template = $http.get('../../App/views/ExamPaper/multi.html');
                    break;
                case 3:
                    template = $http.get('../../App/views/ExamPaper/single.html');
                    break;
                case 4:
                    template = $http.get('../../App/views/ExamPaper/textentry.html');
                    break;
                case 6:
                    template = $http.get('../../App/views/ExamPaper/extendedtext.html');
                    break;
                case 5:
                    template = $http.get('../../App/views/ExamPaper/readingComprehension.html');
                    break;
                case 7:
                    template = $http.get('../../App/views/ExamPaper/cloze.html');
                    break;
            }
            return template;
        };

        return {
            getTemplates: getTemplates
        };
    }]);

    service.factory('examService', ['$http', function ($http) {
        var methods = {
            list: {
                getPaper: function () {
                    return $http.get('GetExamPaper' + '?token=' + Token + '&r=' + Math.random());
                },
                getTime: function () {
                    return $http.get('GetTime' + '?token=' + Token + '&r=' + Math.random());
                },
                saveQuestionAnswer: function (answer) {
                    var url = 'SaveQuestionAnswer' + '?token=' + Token + '&r=' + Math.random();
                    return $http.post(url, answer).success(function (data) {
                        return data;
                    });
                },
                saveAnswer: function (answerList) {
                    var url = 'SaveAnswer' + '?token=' + Token + '&r=' + Math.random();
                    return $http.post(url, answerList).success(function (data) {
                        return data;
                    });
                },
                getExamineeInfo: function () {
                    var action = 'getExamineeInfo' + '?token=' + Token + '&r=' + Math.random();
                    return $http.get(action);
                },
                submitPaper: function (flag, isMandatory) {
                    return $http.post('SubmitPaper?r=' + Math.random(), { token: Token, submitType: flag, isMandatory: isMandatory, r: Math.random() });
                },
                SaveExamineePhoto: function(imgData){
                    var url = '/Examinee/SaveExamineePhoto';
                    return $http.post(url, imgData).success(function (data) {
                        return data;
                    });
                }
            }
        };
        return methods;
    }]);

    service.controller('confirmmModal', ['$scope', '$modalInstance', 'items', function ($scope, $modalInstance, items) {
        var methods = {
            ok: function () {
                $modalInstance.close(true);
            },
            cancel: function () {
                $modalInstance.dismiss('cancel');
            },
            text: items
        };
        $.extend($scope, methods);
    }]);

    service.controller('viewModal', ['$scope', '$modalInstance', 'items', function ($scope, $modalInstance, items) {
        $scope.items = items;
        var methods = {
            ok: function () {
                $modalInstance.close($scope.items.msg);
            },
            onClick: function (curItem, curChoice, curIndex) {
                console.log(11111);
                arrayResult = [];
                var doneCount = 0;
                // 遍历所有小题
                angular.forEach(curItem.subItems, function (subItem, subItemIndex, subItemArray) {
                    // 初始化当前小题为未作答
                    var itemSelected = false;
                    // 当前选中项是否已经选中
                    var hadSelected = curChoice.isSelected;
                    // 遍历小题选项 
                    angular.forEach(subItem.choices, function (subItemChoice, subItemChoiceIndex, subItemChoiceArray) {
                        // 如果是当前作答的小题
                        if (curIndex == subItemIndex) {
                            // 设置为未选中
                            subItemChoice.isSelected = false;
                            // 如果是当前作答的小题的选项
                            if (subItemChoice.identifier == curChoice.identifier) {
                                subItemChoice.isSelected = !hadSelected;
                            }
                        }
                        // 如果当前小题作答了
                        if (subItemChoice.isSelected) {
                            // 已作答小题数加1
                            doneCount++;
                            // 设置当前小题为已作答
                            itemSelected = true;
                            // 将选中项标识添加入作答结果数组    
                            arrayResult.push(subItemChoice.identifier);
                        }
                    });
                    // 如果小题未作答则需要给作答结果数组添加默认空值
                    if (!itemSelected) {
                        arrayResult.push('');
                    }
                });

                // 如果所有小题都作答了，表示该curItem已作答
                curItem.itemCompletionStatus = curItem.subItems.length == doneCount ? 3 : 1;

                // 更新作答
                curItem.candidateResponses = arrayResult;
            },
            onBlur: function (curItem) {
                // ...
            },
            text: items
        };
        $.extend($scope, methods);
    }]);

    service.controller('errorModal', ['$scope', '$modalInstance', 'error', function ($scope, $modalInstance, error) {
        var methods = {
            cancel: function () {
                $modalInstance.dismiss('cancel');
            },
            report: function () {
                $modalInstance.close(true);
            }
        };
        angular.extend($scope, methods, error);
    }]);
})();